package com.zhang.web.servlet;

import com.zhang.domain.Love;
import com.zhang.domain.Page;
import com.zhang.domain.ResultInfo;
import com.zhang.domain.User;
import com.zhang.service.UserService;
import com.zhang.service.impl.UserServiceImpl;
import com.zhang.utils.CookieUtils;
import com.zhang.utils.JsonUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    //注册
    public void regiest(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String[] loveStr = req.getParameterValues("love");
        List<Love> loves = new ArrayList<>();
        for (String loveId : loveStr) {
            Love love = new Love();
            love.setId(Integer.parseInt(loveId));
            loves.add(love);

        }

        User user = new User(0,username,password,email,sex,loves);
        System.out.println(user);


        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        info.setData("注册成功");
        JsonUtils.send(resp,info);
    }

    //登录
    public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String checkCode = req.getParameter("checkCode");
        String remember = req.getParameter("remember");

        HttpSession session = req.getSession();
        ResultInfo info = new ResultInfo();
        String code = (String) session.getAttribute("CHECKCODE_SERVER");
        if(code.equalsIgnoreCase(checkCode) && !"".equals(checkCode)){
          User user = userService.login(username,password);
            System.out.println(user);
          if (user != null){
              session.setAttribute("user",user);
              info.setFlag(true);
              info.setData("登陆成功");

              //创建cookie
              if(null != remember && !"".equals(remember)){
                  CookieUtils.addCookie("remember",req,resp,username,password);
              }


              JsonUtils.send(resp,info);
          }else{
              info.setFlag(false);
              info.setErrorMsg("用户名或密码错误");
              JsonUtils.send(resp,info);
          }
        }else {
            info.setFlag(false);
            info.setErrorMsg("验证码错误");
            JsonUtils.send(resp,info);
        }
    }


    //退出
    public void exist(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().removeAttribute("user");

        Cookie cookie = CookieUtils.getCookie("remember", req);
        cookie.setMaxAge(0);
        cookie.setPath(req.getContextPath());
        resp.addCookie(cookie);

        resp.sendRedirect("/comPro/login.html");

    }


    //显示用户名
    public void getUserName(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if(null != user){
            String username = user.getUsername();
            ResultInfo info = new ResultInfo();
            info.setData(username);
            JsonUtils.send(resp,info);
        }

    }

    //分页+条件查询
    public void queryPage(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        //1.获取参数
        ResultInfo info = new ResultInfo();
        String currentPageStr = req.getParameter("currentPage");
        String rowStr = req.getParameter("row");
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");

        //(1)解决get请求乱码
        username = new String(username.getBytes("iso-8859-1"), "UTF-8");
        email = new String(email.getBytes("iso-8859-1"), "UTF-8");
        sex = new String(sex.getBytes("iso-8859-1"), "UTF-8");


        //(1)当前页+没页条目数
        int currentPage = 1;
        if (null != currentPageStr && !"".equals(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        int row = 5;
        if (null != rowStr && !"".equals(rowStr)) {
            row = Integer.parseInt(rowStr);
        }

        //(2)查询条件
        Map<String, Object> condition = new HashMap<>();
        if (null != username && !"".equals(username) && !"null".equals(username)) {
            condition.put("username", username);
        }
        if (null != email && !"".equals(email) && !"null".equals(email)) {
            condition.put("email", email);
        }
        if (null != sex && !"".equals(sex) && !"null".equals(sex)) {
            condition.put("sex", sex);
        }


        //2.查询分页对象
        Page page = userService.queryPage(condition, currentPage, row);
        info.setFlag(true);
        info.setData(page);
        JsonUtils.send(resp, info);

    }

    //添加用户
    public void addUser(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String[] loveStr = req.getParameterValues("love");
        List<Love> loves = new ArrayList<>();
        for (String loveId : loveStr) {
            Love love = new Love();
            love.setId(Integer.parseInt(loveId));
            loves.add(love);
        }

        User user = new User(0,username,password,email,sex,loves);


        //调用dao
        userService.addUser(user);

        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        info.setData("添加成功");
        JsonUtils.send(resp,info);

    }
    //删除用户
    public void delUser(HttpServletRequest req, HttpServletResponse resp){
        String[] idsStr = req.getParameterValues("ids[]");

        for (int i = 0; i < idsStr.length; i++) {
            userService.delUser(Integer.parseInt(idsStr[i]));
        }

        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        JsonUtils.send(resp,info);

    }



    //修改用户~~~
    //先查询
    public void selectUser(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        User user = userService.findUser(Integer.parseInt(id));

        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        info.setData(user);
        JsonUtils.send(resp,info);
    }
    //后修改
    public void upUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String[] loveStr = req.getParameterValues("love");
        List<Love> loves = new ArrayList<>();
        for (String loveId : loveStr) {
            Love love = new Love();
            love.setId(Integer.parseInt(loveId));
            loves.add(love);

        }
        User user = new User(Integer.parseInt(id),username,password,email,sex,loves);
        userService.updateUser(user);




        resp.sendRedirect("/comPro/user_list.html");

    }


}













